package me.zhengjie.modules.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.MrMaterialDtl;
import me.zhengjie.modules.management.domain.Pageable;
import me.zhengjie.modules.management.mapper.MrMaterialDtlMapper;
import me.zhengjie.modules.management.service.MrMaterialDtlService;
import me.zhengjie.modules.management.service.dto.MrMaterialDtlCriteria;
import me.zhengjie.utils.PageHelpResultUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: eladmin
 * @description: 材料业务逻辑实现
 * @author: ming.cao
 * @create: 2020-12-09 10:02
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true ,rollbackFor = Exception.class)
public class MrMaterialDtlServiceImpl implements MrMaterialDtlService {
    private final MrMaterialDtlMapper mrMaterialDtlMapper;

    @Override
    public Map<String, Object> getAllList(MrMaterialDtlCriteria criteria, Pageable pageable) {
        PageHelper.startPage(pageable.getPage(),pageable.getSize());
        List<MrMaterialDtl> list = mrMaterialDtlMapper.getAllList(criteria);
        //数据分页
        PageInfo<MrMaterialDtl> pageInfo1 = new PageInfo<>(list);
        return PageHelpResultUtil.toPage(pageInfo1);
    }

    @Override
    public Map<String, Object> getMrMaterialDtlById(Integer id) {
        MrMaterialDtl dtl = mrMaterialDtlMapper.getMrMaterialDtlById(id);
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",dtl);
        return map;
    }

    @Override
    public void updateMrMaterialDtl(MrMaterialDtl mrMaterialDtl) {
        try {
            mrMaterialDtl.setModifyDateTime(new Timestamp(new Date().getTime()));
            mrMaterialDtl.setModifyUserId(SecurityUtils.getCurrentUsername());
            mrMaterialDtlMapper.updateMrMaterial(mrMaterialDtl);
        }catch (Exception e){
            throw new BadRequestException("更新失败");
        }

    }

    @Override
    public void addMrMaterialDtl(MrMaterialDtl mrMaterialDtl) {
        try {
            mrMaterialDtl.setCreateDateTime(new Timestamp(new Date().getTime()));
            mrMaterialDtl.setCreateUserId(SecurityUtils.getCurrentUsername());
            mrMaterialDtlMapper.addMrMaterial(mrMaterialDtl);
        }catch (Exception e){
            throw new BadRequestException("新增失败");
        }
    }

    @Override
    @Transactional
    public void delMrMaterialDtl(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                mrMaterialDtlMapper.delMrMaterial(id);
            }
        }catch (Exception e){
            throw new BadRequestException("删除失败");
        }

    }
}
